//
//  TJSingleChatController.m
//  XWC
//
//  Created by Hello on 2022/10/24.
//

#import "HJSingleChatController.h"
#import "HJIM.h"
#import "HJChatMessageLayoutHeader.h"
#import <Photos/Photos.h>


@interface HJSingleChatController ()

@end

@implementation HJSingleChatController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleRefreshUserInfoNotification:) name:refreshUserInfoNotification object:nil];
    [self loadUserInfo];
}

- (void)handleRefreshUserInfoNotification:(NSNotification *)not {
    NSString *sessionId = not.object;
    if ([sessionId isEqualToString:self.sessionModel.sessionId]) {
        [self loadUserInfo];
    }
}


- (void)loadUserInfo {
    
    NSString *userId = TJIM_isSelf(self.sessionModel.senderId) ? self.sessionModel.receiveId : self.sessionModel.uid;
    [[HJIM shareIM].userManager loadFriendInfoWithUserId:userId complete:^(HJChatFriendModel * _Nullable model) {
        
        if (!model) return;
        
        BOOL isUpdate = !([model.avatar isEqualToString:self.sessionModel.sessionAvatar] && [model.nickname isEqualToString:self.sessionModel.sessionName]);
        if (isUpdate) {
            self.sessionModel.sessionAvatar = model.avatar;
            self.sessionModel.sessionName = model.showName;
            [self.sessionModel bg_saveOrUpdateAsync:nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:updateConversationInfoNotification object:self.sessionModel];
        }
    }];
    
}

- (void)didClickMoreButton {
    
}

- (void)sendMediaMessageByFileURLs:(NSArray <NSURL *> *)fileURLs photo:(NSArray <UIImage *> *)photo {
    dispatch_async_queue(^{
        dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
        for (int i = 0; i < fileURLs.count; i++) {
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
            [self sendMediaMessageByFileURL:fileURLs[i] photo:photo[i] completion:^{
                dispatch_semaphore_signal(semaphore);
            }];
        }
    });
}

- (void)sendMediaMessageByAssets:(NSArray<PHAsset *> *)asset photo:(NSArray<UIImage *> *)photo {
    dispatch_async_queue(^{
        dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
        for (int i = 0; i < asset.count; i++) {
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
            [self sendMediaMessageByAsset:asset[i] photo:photo[i] completion:^{
                dispatch_semaphore_signal(semaphore);
            }];
        }
    });
}


- (void)sendTextMessageByText:(NSString *)text {
    
    HJChatMessageModel *messageModel = [[HJChatMessageModel alloc] init];
    messageModel.chatType = HJChatTypeSingle;
    messageModel.content = text;
    messageModel.sessionId = self.sessionModel.sessionId;
    messageModel.receiverId = TJIM_isSelf(self.sessionModel.senderId) ? self.sessionModel.receiveId : self.sessionModel.uid;
    [[HJIM shareIM].chatManager sendTextMessageWithModel:messageModel begin:^(HJChatMessageModel * _Nonnull model) {
        [self updateMessageWithMessageModel:model];
    } completion:^(NSDictionary * _Nullable response, NSError * _Nullable error) {
        
    }];
    
}

- (void)sendMediaMessageByAsset:(PHAsset *)asset photo:(UIImage *)photo completion:(void (^)(void))completion {
    
    HJChatMessageModel *messageModel = [[HJChatMessageModel alloc] init];
    messageModel.chatType = HJChatTypeSingle;
    messageModel.asset = asset;
    messageModel.originalImage = photo;
    messageModel.sessionId = self.sessionModel.sessionId;
    messageModel.receiverId = TJIM_isSelf(self.sessionModel.senderId) ? self.sessionModel.receiveId : self.sessionModel.uid;
    
    //    [[HJIM shareIM].chatManager sendMediaMessageWithModel:messageModel begin:^(HJChatMessageModel * _Nonnull model) {
    //        [self updateMessageWithMessageModel:model];
    //        completion();
    //    } completion:^(NSDictionary * _Nullable response, NSError * _Nullable error) {
    //
    //    }];
}



- (void)sendMediaMessageByFileURL:(NSURL *)fileURL photo:(UIImage *)photo completion:(void (^)(void))completion {
    HJChatMessageModel *messageModel = [[HJChatMessageModel alloc] init];
    
    messageModel.chatType = HJChatTypeSingle;
    messageModel.fileURL = fileURL;
    messageModel.originalImage = photo;
    messageModel.sessionId = self.sessionModel.sessionId;
    messageModel.receiverId = TJIM_isSelf(self.sessionModel.senderId) ? self.sessionModel.receiveId : self.sessionModel.uid;
    [[HJIM shareIM].chatManager sendMediaMessageWithModel:messageModel begin:^(HJChatMessageModel * _Nonnull model) {
        [self updateMessageWithMessageModel:model];
        completion();
    } completion:^(NSDictionary * _Nullable response, NSError * _Nullable error) {
        
    }];
}


- (void)sendImageMessageByLocalPath:(NSString *)localPath {
    HJChatMessageModel *messageModel = [[HJChatMessageModel alloc] init];
    messageModel.chatType = HJChatTypeSingle;
    messageModel.localPath = localPath;
    messageModel.sessionId = self.sessionModel.sessionId;
    messageModel.receiverId = TJIM_isSelf(self.sessionModel.senderId) ? self.sessionModel.receiveId : self.sessionModel.uid;
    [[HJIM shareIM].chatManager sendImageMessageWithModel:messageModel begin:^(HJChatMessageModel * _Nonnull model) {
        [self updateMessageWithMessageModel:model];
    } completion:^(NSDictionary * _Nullable response, NSError * _Nullable error) {
        
    }];
}

- (void)sendVideoMessageByLocalPath:(NSString *)localPath photo:(UIImage *)photo {
    HJChatMessageModel *messageModel = [[HJChatMessageModel alloc] init];
    messageModel.chatType = HJChatTypeSingle;
    messageModel.localPath = localPath;
    messageModel.originalImage = photo;
    messageModel.sessionId = self.sessionModel.sessionId;
    messageModel.receiverId = TJIM_isSelf(self.sessionModel.senderId) ? self.sessionModel.receiveId : self.sessionModel.uid;
    [[HJIM shareIM].chatManager sendVideoMessageWithModel:messageModel begin:^(HJChatMessageModel * _Nonnull model) {
        [self updateMessageWithMessageModel:model];
    } completion:^(NSDictionary * _Nullable response, NSError * _Nullable error) {
        
    }];
}

- (void)sendFileMessageByUrl:(NSURL *)URL {
    HJChatMessageModel *messageModel = [[HJChatMessageModel alloc] init];
    messageModel.chatType = HJChatTypeSingle;
    messageModel.fileURL = URL;
    messageModel.sessionId = self.sessionModel.sessionId;
    messageModel.receiverId = TJIM_isSelf(self.sessionModel.senderId) ? self.sessionModel.receiveId : self.sessionModel.uid;
    [[HJIM shareIM].chatManager sendFileMessageWithModel:messageModel begin:^(HJChatMessageModel * _Nonnull model) {
        [self updateMessageWithMessageModel:model];
    } completion:^(NSDictionary * _Nullable response, NSError * _Nullable error) {
        
    }];
    
}
- (void)sendVoiceMessageByLocalPath:(NSString *)localPath {
    HJChatMessageModel *messageModel = [[HJChatMessageModel alloc] init];
    messageModel.chatType = HJChatTypeSingle;
    messageModel.localPath = localPath;
    messageModel.sessionId = self.sessionModel.sessionId;
    messageModel.receiverId = TJIM_isSelf(self.sessionModel.senderId) ? self.sessionModel.receiveId : self.sessionModel.uid;
    [[HJIM shareIM].chatManager sendVoiceMessageWithModel:messageModel begin:^(HJChatMessageModel * _Nonnull model) {
        [self updateMessageWithMessageModel:model];
    } completion:^(NSDictionary * _Nullable response, NSError * _Nullable error) {
        
    }];
}

- (void)updateMessageWithMessageModel:(HJChatMessageModel *)model {
    HJChatMessageLayout *messageLayout = [HJChatMessageLayout messagelayoutWithMessageModel:model];
    [self.messageController addNewMessage:messageLayout];
}

- (void)dealloc {
    NSLog(@"%s", __func__);
}


@end
